﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

namespace BaifudataPay.Applib
{
    public class RequestUtil
    {
        public static HttpRequest Request { get { return System.Web.HttpContext.Current.Request; } }

        public static object Get(string name,object defv=null)
        {
            object value=Request.QueryString[name];
            return null == value ? defv : value;
        }

        public static object Form(string name, object defv = null)
        {
            object value = Request.Form[name];
            return null == value ? defv : value;
        }

        public static object Params(string name, object defv = null)
        {
            object value = Request.Params[name];
            return null == value ? defv : value;
        }

        public static object Cookie(string name, object defv = null)
        {
            object value = Request.Cookies[name];
            return null == value ? defv : value;
        }

        public static object Session(string name, object defv = null)
        {
            object value = HttpContext.Current.Session[name];
            return null == value ? defv : value;
        }

        public static string GetString(string name, string defv = "")
        {
            object value = Get(name);
            return null == value ? defv : value.ToString();
        }

        public static int GetInteger(string name, int defv = 0)
        {
            int iout;
            return int.TryParse(GetString(name), out iout) ? iout : defv;
        }

        public static decimal GetDecimal(string name, decimal defv = 0)
        {
            decimal iout;
            return decimal.TryParse(GetString(name), out iout) ? iout : defv;
        }

        public static string FormString(string name, string defv = "")
        {
            object value = Form(name);
            return null == value ? defv : value.ToString();
        }

        public static int FormInteger(string name, int defv = 0)
        {
            int iout;
            return int.TryParse(FormString(name), out iout) ? iout : defv;
        }

        public static decimal FormDecimal(string name, decimal defv = 0)
        {
            decimal iout;
            return decimal.TryParse(FormString(name), out iout) ? iout : defv;
        }

        public static string ParamsString(string name, string defv = "")
        {
            object value = Params(name);
            return null == value ? defv : value.ToString();
        }

        public static int ParamsInteger(string name, int defv = 0)
        {
            int iout;
            return int.TryParse(ParamsString(name), out iout) ? iout : defv;
        }
        public static string ToFormSerializeString()
        {
            System.Text.StringBuilder sbld = new System.Text.StringBuilder();
            NameValueCollection data = Request.Form;
            foreach (string name in data.Keys)
            {
                sbld.AppendFormat("&{0}={1}", name, data[name]);
            }
            return sbld.Length > 0 ? sbld.ToString().Substring(1) : string.Empty;
        }
        public static string ToGetSerializeString()
        {
            System.Text.StringBuilder sbld = new System.Text.StringBuilder();
            NameValueCollection data = Request.QueryString;
            foreach (string name in data.Keys)
            {
                sbld.AppendFormat("&{0}={1}", name, data[name]);
            }
            return sbld.Length > 0 ? sbld.ToString().Substring(1) : string.Empty;
        }
    }
}